package com.exmaple.baidumv.fragment;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.baidumv.R;
import com.exmaple.baidumv.adapter.RankingAdapter;
import com.exmaple.baidumv.comm.App;
import com.exmaple.baidumv.entity.RankingEntity;
import com.exmaple.baidumv.thread.BaseHttpThread;
import com.exmaple.baidumv.thread.RankThread;
import com.yuelan.codelib.utils.JsonUtil;
import com.yuelan.codelib.utils.LOG;

public class RankingFragment extends Fragment {

	private View mRootView;
	private ListView mListView;

	private ArrayList<RankingEntity> array ;
	
	private RankingAdapter mAdapter;
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		mRootView = inflater.inflate(R.layout.fragment_ranking, null, false);
		Map<String, String> mpas = App.getUserBaseDeviceInfo(getActivity());
		mpas.put("type", "2");
		new Thread(new RankThread(getActivity(), handler, mpas)).start();
		init();
		return mRootView;
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case BaseHttpThread.PostSuccess:
				JSONObject json;
				try {
					json = new JSONObject((String)msg.obj);
					int code = json.optInt("resultCode", 0);
					if(code == 200){
						JSONArray jsonArray = json.getJSONArray("resultInfo");  
						array= JsonUtil.getRankingJson(jsonArray);
						LOG.v(json +"");
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mAdapter.setList(array);
//				LOG.v(json +"");
				
				break;
			default:
				break;
			}
		};
	};

	private void init() {
		// TODO Auto-generated method stub
		mListView = (ListView) mRootView.findViewById(R.id.list_view);
		mAdapter = new RankingAdapter(getActivity());
		mListView.setAdapter(mAdapter);
	}

}
